package com.cdu.service.Impl;

import com.cdu.commons.ServiceCode;
import com.cdu.commons.ServiceException;
import com.cdu.mapper.FavoriteMapper;
import com.cdu.pojo.dto.TokenUser;
import com.cdu.pojo.entity.Favorite;
import com.cdu.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Override
    public List<Favorite> getFavoriteList(TokenUser tokenUser, Integer page, Integer pageSize) {
        List<Favorite> favoriteList = favoriteMapper.getFavoriteList(tokenUser.getId());
        Integer total = favoriteList.size();
        Integer start = (page-1)*pageSize;
        Integer end = page*pageSize;
        //截取favoriteList的start到end的部分
        if(end > total){
            end = total;
        }
        favoriteList = favoriteList.subList(start, end);
        return favoriteList;
    }


    @Override
    public void cancelFavorite(TokenUser tokenUser, Integer productId) {
      //取消收藏
       int userId = tokenUser.getId();
       //打印userId和productId
       System.out.println("userId:"+userId+" goodsId:"+productId);
      int result= favoriteMapper.cancelFavorite(userId, productId);
//      if(result!= 1) {
//          throw new ServiceException("删除数据库失败", ServiceCode.INSERT_ERROR);
//      }
        System.out.println("取消收藏成功");
        //打印result
        System.out.println("result:"+result);

    }
}
